package org.dtrd.modules.evaluation.controller;


import com.baomidou.mybatisplus.core.metadata.IPage;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.dtrd.base.BaseController;
import org.dtrd.common.api.vo.Result;
import org.dtrd.common.aspect.annotation.AutoLog;
import org.dtrd.modules.evaluation.entity.bean.nrs.NrsDetail;
import org.dtrd.modules.evaluation.entity.bean.nrs.NrsListInfo;
import org.dtrd.modules.evaluation.entity.bean.nrs.NrsListRequest;
import org.dtrd.modules.evaluation.entity.bean.nrs.NrsPageRequest;
import org.dtrd.modules.evaluation.service.IDtrdEntEvaluationRdNrsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

import static org.dtrd.common.constant.CommonConstant.*;
import static org.dtrd.config.system.SystemState.ResponseState.STATE_OPERATION_FAILURE;
import static org.dtrd.config.system.SystemState.ResponseState.STATE_PARAMS_MISSING;

/**
 * <p>
 *  前端控制器
 * </p>
 * @author xiao
 * @since 2022-08-15
 */
@Api(tags = "量表评估")
@RestController
@RequestMapping("/evaluation/nrs/")
public class NrsController extends BaseController {

    @Autowired
    private IDtrdEntEvaluationRdNrsService nrsService;

    @ApiOperation(value = "获取最新的一条NRS(营养筛查评估表)详情", response = NrsListInfo.class)
    @GetMapping("get")
    @AutoLog(value="获取最新的一条NRS(营养筛查评估表)详情", operateType = OPERATE_TYPE_QUERY)
    public Result<?> getNrs(@RequestParam Integer patientId){
        NrsListInfo info = nrsService.getCurrentNrs(patientId);
        return Result.OK(info);
    }

    @ApiOperation(value = "查询NRS(营养筛查评估表)列表", response = NrsListInfo.class)
    @GetMapping("list")
    @AutoLog(value="查询NRS(营养筛查评估表)列表", operateType = OPERATE_TYPE_QUERY)
    public Result<?> getNrsList(@RequestParam("patientId") Integer patientId){
        List<NrsListInfo> list = nrsService.getNrsList(patientId);
        return Result.OK(list);
    }

//    @ApiOperation(value = "分页查询列表", response = NrsListInfo.class)
//    @PostMapping("")
//    @AutoLog(value="查询列表", operateType = OPERATE_TYPE_QUERY)
//    public Result<?> getNrsList(@RequestBody NrsPageRequest request){
//        handlePageRequest(request);
//        IPage<NrsListInfo> page = nrsService.pageNrs(request);
//        return Result.OK(page);
//    }

    @ApiOperation("新增或修改NRS(营养筛查评估表)")
    @PostMapping("save")
    @AutoLog(value="新增或修改NRS(营养筛查评估表)", operateType = OPERATE_TYPE_SAVE_OR_UPDATE)
    public Result<?> saveOrUpdateNrs(@RequestBody @Validated NrsDetail dto){
        boolean result = nrsService.saveOrUpdateNrs(dto);
        return result ? Result.OK() : error(STATE_OPERATION_FAILURE);
    }

    @ApiOperation("删除NRS(营养筛查评估表)")
    @DeleteMapping("delete")
    @AutoLog(value="删除NRS(营养筛查评估表)", operateType = OPERATE_TYPE_DELETE)
    public Result<?> removeNrs(@RequestParam Integer id){
        if(isIntParamInvalid(id)) {
            return error(STATE_PARAMS_MISSING);
        }
        boolean result = nrsService.removeNrs(id);
        return result ? Result.OK() : error(STATE_OPERATION_FAILURE);
    }
}
